Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: De-couple Chips from a specific ExecutionRecord, part II #37

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

huitseeker
Copy link
Contributor

Previously: #9

This applies the same logic to output events in generate_trace.

General approach

// implemented on the chip
trait WithEvents<'a> {
  type InputEvents : 'a;
  type OutputEvents: 'a;
}

// implemented on the record
trait EventLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a self) -> <T as WithEvents<'a>>::InputEvents;
}
// implemented on the record
trait EventMutLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a mut self, events: <T as WithEvents<'a>>::OutputEvents);
}

Now, one would wish that this would be a bit more ergonomic in Rust, because we do have anonymous cartesian products of arbitrary arity (tuples), but we do not have anonymous coproducts - either crate notwithstanding.

DivRemChip Example

pub enum DivRemEvent<'a> {
    ByteLookupEvent(&'a ByteLookupEvent),
    MulEvent(&'a AluEvent),
    LtEvent(&'a AluEvent),
}

impl<'a> WithEvents<'a> for DivRemChip {
    type InputEvents = &'a [AluEvent];
    type OutputEvents =  [DivRemEvent<'a>];
}

impl EventMutLens<DivRemChip> for ExecutionRecord {
    fn add_events(&mut self, events: <DivRemChip as crate::air::WithEvents<'_>>::OutputEvents) {
        for event in events {
            match event {
                DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(*e),
                DivRemEvent::MulEvent(e) => self.add_mul_event(*e),
                DivRemEvent::LtEvent(e) => self.add_lt_event(*e),
            }
        }
    }
}

Previously: #9

This applies the same logic to output events in `generate_trace`.

```rust
// implemented on the chip
trait WithEvents<'a> {
  type InputEvents : 'a;
  type OutputEvents: 'a;
}

// implemented on the record
trait EventLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a self) -> <T as WithEvents<'a>>::InputEvents;
}
// implemented on the record
trait EventMutLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a mut self, events: <T as WithEvents<'a>>::OutputEvents);
}
```

Now, one would wish that this would be a bit more ergonomic in Rust, because we do have anonymous cartesian products of arbitrary arity (tuples), but we do not have anonymous coproducts - either crate notwithstanding.

```rust
pub enum DivRemEvent<'a> {
    ByteLookupEvent(&'a ByteLookupEvent),
    MulEvent(&'a AluEvent),
    LtEvent(&'a AluEvent),
}

impl<'a> WithEvents<'a> for DivRemChip {
    type InputEvents = &'a [AluEvent];
    type OutputEvents =  [DivRemEvent<'a>];
}

impl EventMutLens<DivRemChip> for ExecutionRecord {
    fn add_events(&mut self, events: <DivRemChip as crate::air::WithEvents<'_>>::OutputEvents) {
        for event in events {
            match event {
                DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(*e),
                DivRemEvent::MulEvent(e) => self.add_mul_event(*e),
                DivRemEvent::LtEvent(e) => self.add_lt_event(*e),
            }
        }
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant